package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

/**
 * @author 小李要吃鱼
 * @date 2021-01-26 18:15
 */
public class Planet extends Star {
    private int longAxis;
    private  int shortAxis;
    public double speed;
    public  double theta;
    public  Star star;
    public String name;
    public boolean smallPlanet;

    public boolean venus;



    public  Planet(){

    }
    public  Planet(Star star,String imageKey,double AU,double e,int T,String name){
        this.star = star;
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxisByAU(AU);
        this.shortAxis =getShortAxisByE(e);
        this.x = Constant.WINDOW_WIDTH / 2 + longAxis;
        this.y = Constant.WINDOW_HEIGHT / 2;
        this.speed = getSpeedByT(T);
        this.theta = 0.0;
        this.name = name;

    }
    public  Planet(Star star,String imageKey,double AU,double e,int T,String name,boolean smallPlanet) {
        this(star,imageKey,AU,e,T,name);
        this.smallPlanet = smallPlanet;
    }
    public  Planet(Sun sun,String imageKey,double AU,double e,int T,String name,boolean smallPlanet,boolean venus) {
        this(sun,imageKey,AU,e,T,name,smallPlanet);
        this.venus = venus;
    }
    private double getSpeedByT(int t){
        return 0.1 * 365 / t;

    }
    private int getLongAxisByAU(double AU){
        return (int) (100 * AU);
    }
    private int getShortAxisByE(double e){
        return (int)(this.longAxis * Math.sqrt(1 - e * e)) / 2;
    }

    @Override
    public void draw(Graphics g) {


      Color c = g.getColor();
        g.setColor(Color.white);
        if(!smallPlanet){
            drawTrace(g);
        }

//        this.drawTrace(g);
        Font f = g.getFont();
        g.setFont(new Font("黑体",Font.BOLD,24));
//        g.setColor(c);
        g.drawString(name,x,y - 30);
        g.setFont(f);
        g.setColor(c);

        g.drawImage(img,x,y,null);

        move();
    }
    private void drawTrace(Graphics g){
        int x =this.star.x - this.longAxis + star.width / 2;
        int y = this.star.y - this.shortAxis + star.height / 2;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y,width,height);

    }

    @Override
    public void move() {
        if(!venus){
            x = (int)(longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2;
            y = (int)(shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
        }else {
            x = (int) (longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2;
            y = (int) (shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        }
        theta += speed;
    }

//    public  void draw(Graphics g) {
//        g.drawImage(img,x,y,null);
//
//    }
//    public void move(){
//        x = (int)(longAxis * Math.cos(theta)) + Constant.WINDOW_WIDTH / 2;
//        y = (int)(shortAxis * Math.sin(theta)) + Constant.WINDOW_HEIGHT /2;
//        theta += speed;
//
//    }
}
